package whf.framework.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

import org.apache.commons.configuration.Configuration;

import whf.framework.util.StringUtils;

/**
 * @author wanghaifeng
 *
 */
public abstract class BaseConfiguration implements whf.framework.config.Configuration{
	
	protected Configuration configuration;
	
	public double getDouble(String key, double defaultValue) {
		try{
			return Double.parseDouble(getString(key));
		}catch(Exception e){
			return defaultValue;
		}
	}

	public int getInt(String key, int defaultValue) {
		try{
			return Integer.parseInt(getString(key));
		}catch(Exception e){
			return defaultValue;
		}
	}
	
	public String[] getStringArray(String key){
		List<String> list = getList(key);
		String[] res = new String[list.size()];
		list.toArray(res);
		return res;
	}
	
	public final String getString(String key){
		return configuration.getString(key);
	}
	
	public final boolean getBoolean(String key){
		try{
			return configuration.getBoolean(key);
		}catch(NoSuchElementException e){
			return false;
		}
	}
	
	public final List<String> getList(String key){
		List list = configuration.getList(key);
		List<String> stringList = new ArrayList<String>();
		for(Iterator it = list.iterator(); it.hasNext(); ){
			String s = (String)it.next();
			if(!StringUtils.isEmpty(s)){
				stringList.add(s);
			}
		}
		return stringList;
	}
}
